package com.hospital.controller;

import com.hospital.common.Result;
import com.hospital.common.UserRole;
import com.hospital.entity.User;
import com.hospital.service.AuthService;
import com.hospital.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 认证控制器
 *
 * @author Hospital Management System
 */
@Api(tags = "用户认证")
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
public class AuthController {

    private final AuthService authService;
    private final UserService userService;

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<Void> register(@Validated @RequestBody User user) {
        authService.register(user);
        return Result.success(null, "注册成功");
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<Map<String, Object>> login(
            @ApiParam("用户名") @RequestBody Map<String, String> loginRequest) {
        
        String username = loginRequest.get("username");
        String password = loginRequest.get("password");
        String roleStr = loginRequest.get("role");
        
        Integer role = null;
        if (roleStr != null && !roleStr.trim().isEmpty()) {
            role = Integer.parseInt(roleStr);
        }
        
        Map<String, Object> result = authService.login(username, password, role);
        return Result.success(result, "登录成功");
    }

    @ApiOperation("获取用户信息")
    @GetMapping("/user-info")
    public Result<Map<String, Object>> getUserInfo() {
        User user = authService.getCurrentUser();
        if (user == null) {
            return Result.error("用户未登录");
        }
        
        Map<String, Object> profile = new HashMap<>();
        profile.put("userInfo", user);
        
        // 根据用户角色添加特定信息
        switch (user.getRole()) {
            case 1: // UserRole.PATIENT
                // 患者信息已在userInfo中
                break;
            case 2: // UserRole.DOCTOR
                User doctorDetail = userService.getDoctorDetail(user.getId());
                profile.put("doctorInfo", doctorDetail);
                break;
            case 3: // UserRole.ADMIN
                // 管理员信息已在userInfo中
                profile.put("adminInfo", user);
                break;
        }
        
        return Result.success(profile);
    }

    @ApiOperation("更新用户基本信息")
    @PutMapping("/profile")
    public Result<Void> updateProfile(@Validated @RequestBody User user) {
        User currentUser = authService.getCurrentUser();
        user.setId(currentUser.getId());
        userService.updateById(user);
        return Result.success(null, "信息更新成功");
    }

    @ApiOperation("更新医生信息")
    @PutMapping("/profile/doctor")
    public Result<Void> updateDoctorProfile(@Validated @RequestBody User doctor) {
        User currentUser = authService.getCurrentUser();
        if (!UserRole.DOCTOR.equals(currentUser.getRole())) {
            return Result.error("权限不足，仅医生可更新医生信息");
        }
        
        doctor.setId(currentUser.getId());
        doctor.setRole(UserRole.DOCTOR);
        userService.updateById(doctor);
        return Result.success(null, "医生信息更新成功");
    }

    @ApiOperation("更新管理员信息")
    @PutMapping("/profile/admin")
    public Result<Void> updateAdminProfile(@Validated @RequestBody User admin) {
        User currentUser = authService.getCurrentUser();
        if (!UserRole.ADMIN.equals(currentUser.getRole())) {
            return Result.error("权限不足，仅管理员可更新管理员信息");
        }
        
        admin.setId(currentUser.getId());
        admin.setRole(UserRole.ADMIN);
        userService.updateById(admin);
        return Result.success(null, "管理员信息更新成功");
    }

    @ApiOperation("修改密码")
    @PutMapping("/password")
    public Result<Void> changePassword(@RequestBody Map<String, String> passwordRequest) {
        User currentUser = authService.getCurrentUser();
        if (currentUser == null) {
            return Result.error("用户未登录");
        }
        
        String oldPassword = passwordRequest.get("oldPassword");
        String newPassword = passwordRequest.get("newPassword");
        authService.changePassword(currentUser.getId(), oldPassword, newPassword);
        return Result.success(null, "密码修改成功");
    }

    @ApiOperation("用户登出")
    @PostMapping("/logout")
    public Result<Void> logout() {
        User currentUser = authService.getCurrentUser();
        if (currentUser == null) {
            return Result.error("用户未登录");
        }
        
        authService.logout();
        return Result.success(null, "登出成功");
    }
} 